/*
 * Project name:
     EasyPIC LCD Potentiometer
 * Author:
     Samuel.B.Atlan@gmail.com
 * Revision History:
     2010/05/25: initial release
 * Description:
     Simple Display of ADC potentiometer value on the in build LCD displayer
 * Test configuration:
     MCU:             PIC16F887
     Dev.Board:       EasyPIC6
     Oscillator:      HS, 8.0000 MHz
     Ext. Modules:    -
     SW:              mikroC PRO for PIC 3.2
 * NOTES:
     - J15 on RA2 to use potentiometer
     - Value unknownly goes from 788 to 1015 on LCD display...
*/

const unsigned long waiting = 200;
char *text = "Hello World :-)";
long potentiometer_value;

// Port Expander module connections
  sbit  SPExpanderCS  at RA2_bit;
  sbit  SPExpanderRST at RA3_bit;
  sbit  SPExpanderCS_Direction  at TRISA2_bit;
  sbit  SPExpanderRST_Direction at TRISA3_bit;
// End Port Expander module connections

void init() {
  ANSEL  = 0x04;                            // Configure AN2 pin as analog
  ANSELH = 0;                               // Configure other AN pins as digital I/O
  C1ON_bit = 0;                             // Disable comparators
  C2ON_bit = 0;

// Port Expander Library uses SPI1 module
  SPI1_Init();                              // Initialize SPI module used with PortExpander
  SPI_Lcd_Config(0);                        // Initialize Lcd over SPI interface
  SPI_Lcd_Cmd(_LCD_CLEAR);                  // Clear display
  SPI_Lcd_Cmd(_LCD_CURSOR_OFF);             // Turn cursor off
}

void main() {
  init();
  while(1) {
    potentiometer_value = ADC_Read(2);      // Get 10-bit results of AD conversion
    LongToStr(potentiometer_value,text);     // Convert Unsigned Int to Str
    SPI_Lcd_Out(1,1, text);                 // Print text to Lcd, row 1, column 1
//    Delay_ms(waiting);
  }
}
